package modelo;


	import java.sql.*;
	import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JComboBox;

	public class Conexion {
	    public Connection conexion;
	    public Conexion()
	    {
	        abrir();
	    }
	    public Object getCodigo(String tabla,String filtro)
	    {
	        Object val="";
	        try
	        {
	            ResultSet rs= this.consulta("select *from `"+ tabla +"` where "+filtro);
	            if (rs.next())
	                val=rs.getObject(1);
	        }
	        catch(Exception e)
	        {
	            System.out.println(e);
	        }
	        return val;
	    }

	    public void llenarCombo(JComboBox c,String sql)
	    {
	        try
	        {
	            c.removeAllItems();
	            c.addItem("[Elija una Opcion]");
	            ResultSet rs = this.consulta(sql);
	            while (rs.next())
	            {
	                c.addItem(""+rs.getString(1));
	            }
	        }
	        catch (Exception e)
	        {
	            System.out.println(e);
	        }
	    }
	    public void cerrar()
	        {
	        try {
	            conexion.close();
	        } catch (SQLException ex) {
	            Logger.getLogger(Conexion.class.getName()).log(Level.SEVERE, null, ex);
	        }
	        }
	    public void abrir()
	        {
	                   try
	        {
	            Class.forName("org.postgresql.Driver");
	            conexion = DriverManager.getConnection("jdbc:postgresql://localhost:5433/BD5-4", "postgres", "postgres");

	        }
	       catch(Exception e)
	       {
	           System.out.println(e);
	       }


	        }
	    public boolean ejecutar(String sql)
	    {
	    	boolean eje=false;
	     try
	     {
	        Statement st = conexion.createStatement();
	        st.execute(sql);
	        eje=true;


	     }
	     catch(Exception e)
	     {
	         System.out.println(e+"");
	         System.out.println(sql);
	     }
	     return eje;

	    }

	    public ResultSet consulta(String sql)throws SQLException
	    {
	     Statement st = conexion.createStatement();
	     ResultSet rs= st.executeQuery(sql);
	     return rs;
	    }

	}



